package com.jin.webvideo.service;

import com.jin.webvideo.entity.Cache;
import com.jin.webvideo.entity.VideoInfo;
import com.jin.webvideo.entity.Parser;
import com.jin.webvideo.exception.ParsingFailedException;
import com.jin.webvideo.mapper.ParserMapper;
import com.jin.webvideo.mapper.VideoMapper;
import com.jin.webvideo.parser.ParseResult;
import com.jin.webvideo.parser.ParserManage;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;
import com.jin.webvideo.util.sys.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParserService {
    @Autowired
    ParserManage manage;
    @Autowired
    ParserMapper parserMapper;
    @Autowired
    CacheService cacheService;
    @Autowired
    VideoMapper videoMapper;

    /**
     * 根据平台匹配 解析器
     * @param platform
     * @return
     */
    public List<Parser> findParser(String platform){
        return manage.match(platform, null);
    }

    /**
     * 解析出真实视频信息
     * @param request
     * @param platform
     * @param url
     * @param name
     * @return
     * @throws UnsupportedEncodingException
     * @throws ParsingFailedException
     */
    public ParseResult parseRealUrl(HttpServletRequest request, String platform, String url, String name) throws UnsupportedEncodingException, ParsingFailedException {
        String standardizedUrl = URLUtil.urlNoProtocol(url);
        url = URLUtil.https(url);
        Cache cache = cacheService.findCacheByUrl(standardizedUrl);
        // 存在非del状态的cache且未指定解析器时，使用缓存
        if (null != cache && name.isEmpty()){
            if (cache.getStatus().equals(Cache.STATUS_NEW)){
                return scriptParser(request, url, name, platform);
            }
            String host = request.getServerName()+":"+request.getServerPort();
            String path = cacheService.createVisitedPath(cache);
            ParseResult result = new ParseResult("cache", "//" + host + path);
            result.setType(URLUtil.fileType(path));
            return result;
        }
        ParseResult result = scriptParser(request, url, name, platform);
        cache = cacheService.needCache(result, standardizedUrl);
        if (null != cache){
            String source = result.getRelURL();
            cacheService.newCache(source, cache);
        }
        return result;
    }

    // 脚本解析系统
    public ParseResult scriptParser(HttpServletRequest request, String url, String name, String platform) throws ParsingFailedException {
        VideoInfo videoInfo = videoMapper.findVideoInfoByUrl(url);
        ParseResult result = manage.scriptParser(videoInfo, platform, url, name, request);
        if (!ParseResult.isVaild(result)){
            throw new ParsingFailedException("解析失败");
        }
        return result;
    }
}
